package com.dao.Impl;

import com.dao.StaffDao;
import com.entity.PageBean;
import com.entity.Staff;
import com.util.JDBC;
import com.util.TransBean;

import java.util.List;
import java.util.Map;

public class StaffDaoImpl implements StaffDao {
    @Override
    public int insert(Staff staff) {
        return JDBC.sqlInsert("insert into " +
                        "staff(staffName,phone,idCard,address,createdTime,status,roleId,momo,password)" +
                        "VALUES(?,?,?,?,NOW(),?,?,?,123123);",
                staff.getStaffName(), staff.getPhone(), staff.getIdCard(),
                staff.getAddress(), staff.getStatus(), staff.getRoleId(), staff.getMomo());
    }

    @Override
    public int update(Staff staff) {
        return JDBC.sqlUpdate("update staff set staffName=?,phone=?,idCard=?" +
                        " ,address=?,status=?,roleId=?,momo=?" +
                        " where staffId=?",
                staff.getStaffName(), staff.getPhone(), staff.getIdCard(), staff.getAddress(),
                staff.getStatus(), staff.getRoleId(), staff.getMomo(), staff.getStaffId());
    }

    @Override
    public int delect(int id) {
        return JDBC.sqlUpdate("delete from staff where staffId=?", id);
    }

    @Override
    public void findAllStaff(PageBean<Staff> pageBean) {
        String sql = "select count(*) from staff where 1=1 %s ";
        String whereStr = "";
        String[] whereArr = pageBean.getFactor();
        if (whereArr != null) {
            for (String s : whereArr) {
                whereStr += " and " + s;
            }
        }
        sql = String.format(sql, whereStr);
        List<List> lists = JDBC.selectArray(sql);
        if (lists != null) {
            Integer count = ((Long) lists.get(0).get(0)).intValue();
            pageBean.setRowCount(count);
        }

        sql = "select id,staffId,staffName,password,phone,idCard,address,createdTime,status,roleId,momo,enable from staff " +
                "where 1=1 %s limit ?,?";
        sql = String.format(sql, whereStr);
        List<Map> maps = JDBC.selectMap(sql, (pageBean.getPageNo() - 1) * pageBean.getPageSize(), pageBean.getPageSize());
        if (maps != null) {
            pageBean.setData(TransBean.populate(Staff.class, maps));
        }
    }

    @Override
    public List<Staff> findAll() {
        String sql = "select id,staffId,staffName,password,phone,idCard,address,createdTime,status,roleId,momo,enable from staff";
        List<Map> maps = JDBC.selectMap(sql);
        return TransBean.populate(Staff.class, maps);
    }


    @Override
    public Staff findStaffIdPass(int staffId, String pass) {
        List<Map> map = JDBC.selectMap("select id,staffId,staffName,password,phone,idCard,address,status,roleId,momo,createdTime" +
                " from staff where staffId=? and password=? and status=1", staffId, pass);
        List<Staff> list = TransBean.populate(Staff.class, map);
        if (list!=null) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Staff findStaffId(int staffId) {
        List<Map> map = JDBC.selectMap("select id,staffId,staffName,phone,idCard,address,status,roleId,momo" +
                " from staff where staffId=?", staffId);
        List<Staff> list = TransBean.populate(Staff.class, map);
        if (list!=null&&list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Staff> findStaffId(String staffName) {
        List<Map> map = JDBC.selectMap("select id,staffId,staffName,phone,idCard,address,status,roleId,momo from staff where staffName like ?", "%" + staffName + "%");
        List<Staff> list = TransBean.populate(Staff.class, map);
        return list;
    }

    @Override
    public int updatestaffId(int staffId, int id) {
        return JDBC.sqlUpdate("update staff set staffId=? where id=?",
                staffId, id);
    }

    @Override
    public int updatePass(Staff staff) {
        return JDBC.sqlUpdate("update staff set password=? where id=?", staff.getPassword(), staff.getId());
    }

    @Override
    public int maxId() {
        return (int) JDBC.selectArray("select max(staffId) from staff").get(0).get(0);
    }

}
